package com.lej.app_main.test

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.*
import com.lej.app_main.R
import com.lej.app_main.webJs.HomeJavascriptInterface
import com.lej.core.base.statusHeight
import com.lej.core.common.PCacheInfo
import com.lej.core.common.WEB_SHOW_TITLE
import com.lej.core.common.WEB_URL
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.router
import com.lej.core.webView.jsInstances
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import java.lang.reflect.Field

class TestActivity : AppCompatActivity() {
    lateinit var ll:LinearLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        jsInstances.add(::HomeJavascriptInterface)
        PCacheInfo.token =
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJpYXQiOjE2NTUyNjIzMjMsImV4cCI6MTY2MzAzODMyMywibmJmIjoxNjU1MjYyMzIzLCJqdGkiOiI1WWF1amFoQWNJRXNCNmdFIiwic3ViIjoxODk3MCwicHJ2IjoiMTRhOTJjMDg3NDZhMzdhOWZiMTFhMDMyMDg1MGM5OWE5Zjg5ODczOCJ9.EuNiQZPwDrEtpNLGN_I5T_KbR7QPbd0qCC5TmzO5QDY"
        QMUIStatusBarHelper.translucent(this)
        setContentView(R.layout.appmain_activity_test)
        ll = findViewById(R.id.ll)
        hideSystemUI()
        findViewById<EditText>(R.id.tv_test_input).updatePadding(left = statusHeight)
        val enter = findViewById<Button>(R.id.bt_enter)
//        enter.text = getStatusBarHeight(this).toString()
        enter.onClick {
//            val text = findViewById<EditText>(R.id.tv_test_input).text.toString()
//            val text = "http://10.8.82.22:8080/?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJpYXQiOjE2NTUyNjIzMjMsImV4cCI6MTY2MzAzODMyMywibmJmIjoxNjU1MjYyMzIzLCJqdGkiOiI1WWF1amFoQWNJRXNCNmdFIiwic3ViIjoxODk3MCwicHJ2IjoiMTRhOTJjMDg3NDZhMzdhOWZiMTFhMDMyMDg1MGM5OWE5Zjg5ODczOCJ9.EuNiQZPwDrEtpNLGN_I5T_KbR7QPbd0qCC5TmzO5QDY"
            val text = "http://10.8.82.22:8080/order/list"
            router(CRouter.webView) {
                withString(WEB_URL, text)
                withBoolean(WEB_SHOW_TITLE, false)
            }
        }
//        setContent {
//            Text(text = "this is android!")
//        }
    }

//    private fun hideSystemUI() {
//        WindowCompat.setDecorFitsSystemWindows(window, false)
//        WindowInsetsControllerCompat(window, ll).let { controller ->
//            controller.hide(WindowInsetsCompat.Type.systemBars())
//            controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
//        }
//    }


    private fun hideSystemUI() {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }

//    @Preview(showBackground = false)
//    @Composable
//    fun DefaultPreview() {
//        Text(text = "this is android!")
//    }

    fun getStatusBarHeight(context: Context): Int {
        var c: Class<*>?
        var obj: Any? = null
        var field: Field? = null
        val x: Int = 0
        var statusBarHeight = 0
        try {
            c = Class.forName("com.android.internal.R\$dimen")
            obj = c.newInstance()
            field = c.getField("status_bar_height")
            val x = field[obj].toString().toInt()
            statusBarHeight = context.resources.getDimensionPixelSize(x)
        } catch (var7: Exception) {
            var7.printStackTrace()
        }
        return statusBarHeight
    }
}